package com.hst.web.base.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.web.base.entity.TFlowHistory;
import com.hst.web.base.service.PersonCardService;
import com.hst.web.base.ui.CompanyEmployee;
import com.hst.web.base.ui.FlowHistory;
import com.hst.web.base.ui.FlowTask;
import com.hst.web.base.ui.PersonCardApprove;

@Controller
@RequestMapping("/personcard")
public class PersonCardController {
	
	@Autowired
	ISQLDao sql;

	@Autowired
	IORDao dao;
	
	@Autowired
	PersonCardService service;
	
	@SuppressWarnings({ "unchecked" })
	private Map<String, Object> parseQuerys(String data) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> querys = JsonUtil.read(data, Map.class);
		return querys;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/listHistory")
	public void listHistory(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = parseQuerys(data);
		String employeeCode = (String) params.get("employeeCode");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("employeeCode", employeeCode);
		List<FlowHistory> list = (List<FlowHistory>) sql.query("person.listHistory",map);
		m.addAttribute("historys",JsonUtil.toString(list));
	}
	
	@RequestMapping("/applyCard")
	public void applyCard(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String,Object> params = parseQuerys(data);
		String cardType = (String) params.get("cardType");
		String remark = (String) params.get("remark");
		String employeeStr = (String) params.get("employees");
		List<CompanyEmployee> emps = JsonUtil.readList(employeeStr, CompanyEmployee.class);
		service.applyCard(cardType, remark, emps, m);
	}
	
	@RequestMapping("/listApplyDetail")
	public void listApplyDetail(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		PersonCardApprove approve = JsonUtil.read(data, PersonCardApprove.class);
		service.listApplyDetail(approve, m);
	}
	
	@RequestMapping("/listApplyDetailView")
	public void listApplyDetailView(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		PersonCardApprove approve = JsonUtil.read(data, PersonCardApprove.class);
		service.listApplyDetailView(approve, m);
	}
	
	@RequestMapping("/back")
	public void back(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String,Object> params = parseQuerys(data);
		String approveStr = (String) params.get("task");
		String remark = (String) params.get("remark");
		PersonCardApprove approve = JsonUtil.read(approveStr, PersonCardApprove.class);
		service.back(approve,remark,m);
	}
	
	@RequestMapping("/pass")
	public void pass(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String,Object> params = parseQuerys(data);
		String approveStr = (String) params.get("task");
		String remark = (String) params.get("remark");
		PersonCardApprove approve = JsonUtil.read(approveStr, PersonCardApprove.class);
		service.pass(approve, m, remark);
	}
	
	@RequestMapping("/taskHistory")
	public void taskHistory(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		FlowTask task = JsonUtil.read(data, FlowTask.class);
		List<ORQuery> querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq,"taskId",task.getId()));
		querys.add(new ORQuery(Op.order,"dt","asc"));
		List<TFlowHistory> historys = dao.list(TFlowHistory.class,querys);
		m.addAttribute("historys",JsonUtil.toString(historys));
	}
	
	@RequestMapping("/backCheckStatus")
	public void backCheckStatus(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		List<CompanyEmployee> employees = JsonUtil.readList(data, CompanyEmployee.class);
		service.backCheckStatus(employees, m);
	}
}
